﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

using SimbaRGVMonitor.Models;

namespace SimbaRGVMonitor.Views.Components
{
    public partial class RobotItem : UserControl, IRectable
    {
        public RobotItem()
        {
            InitializeComponent();

            backgroundBrush = vehicleGrid.Background;

            InitializeSettings();
        }

        //====================================================================================================
        //以上是类型构造函数
        //----------------------------------------------------------------------------------------------------
        //以下是类型字段
        //====================================================================================================

        private Robot robot;
        private readonly Brush backgroundBrush;
        private RobotStatus status = RobotStatus.Normal;
        private RobotPacketStatus packetStatus = RobotPacketStatus.Empty;

        //====================================================================================================
        //以上是类型字段
        //----------------------------------------------------------------------------------------------------
        //以下是类型属性
        //====================================================================================================

        internal string Uri
        {
            get;
            set;
        }

        internal Point Location
        {
            get;
            private set;
        }

        internal RobotStatus Status
        {
            get { return status; }
            set
            {
                status = value;
                ResetStatusColor();
            }
        }

        internal RobotPacketStatus PacketStatus
        {
            get { return packetStatus; }
            set
            {
                packetStatus = value;
                ResetPacketStatusColor();
            }
        }

        internal double Yaw
        {
            get { return robot == null ? 0d : robot.Yaw; }
        }

        internal string RobotNo
        {
            get { return robot == null ? string.Empty : robot.RobotNo; }
        }

        //====================================================================================================
        //以上是类型属性
        //----------------------------------------------------------------------------------------------------
        //以下是初始化设置
        //====================================================================================================

        private void InitializeSettings()
        {
            Canvas.SetZIndex(this, 3);

            carInfoBorder.MouseEnter += WhenCarInfoBorderMouseEnter;
            carInfoBorder.MouseLeave += WhenCarInfoBorderMouseLeave;

            testMenuItem.Click += WhenTestMenuItemClick;
            restoreMenuItem.Click += WhenRestoreMenuItemClick;
            forbiddenMenuItem.Click += WhenForbiddenMenuItemClick;
            unforbiddenMenuItem.Click += WhenUnforbiddenMenuItemClick;
        }

        //====================================================================================================
        //以上是界面控件事件侦听
        //----------------------------------------------------------------------------------------------------
        //以下是重置小车对象
        //====================================================================================================

        internal void ResetRobot(Robot robot, Point location)
        {
            this.robot = robot;

            Location = location;
            var robotNo = robot.RobotNo;
            Name = "Current" + robotNo;
            vehicleNoTextBlock.Text = robotNo;
            PacketStatus = RobotPacketStatus.HasPack;
            Status = GetRobotStatus(robot.TaskStatus);
            
            this.SetCanvasLocation(Location);
        }

        //====================================================================================================
        //以上是重置小车对象
        //----------------------------------------------------------------------------------------------------
        //以下是界面控件事件侦听
        //====================================================================================================

        private void WhenCarInfoBorderMouseEnter(object sender, RoutedEventArgs e)
        {
            vehicleGrid.Background = Brushes.BlueViolet;
        }

        private void WhenCarInfoBorderMouseLeave(object sender, RoutedEventArgs e)
        {
            vehicleGrid.Background = backgroundBrush;
        }

        private void WhenTestMenuItemClick(object sender, RoutedEventArgs e)
        {

        }

        private void WhenRestoreMenuItemClick(object sender, RoutedEventArgs e)
        {
            status = RobotStatus.Normal;
        }

        private void WhenForbiddenMenuItemClick(object sender, RoutedEventArgs e)
        {
            status = RobotStatus.Forbidden;
        }

        private void WhenUnforbiddenMenuItemClick(object sender, RoutedEventArgs e)
        {
            status = RobotStatus.Normal;
        }

        //====================================================================================================
        //以上是界面控件事件侦听
        //----------------------------------------------------------------------------------------------------
        //以下是获取车辆状态
        //====================================================================================================

        private RobotStatus GetRobotStatus(string taskStatus)
        {
            switch (taskStatus)
            {
                case "TASK_TAKEN":
                    return RobotStatus.Executing;
                case "TASK_FINISHED":
                    return RobotStatus.Normal;
                case "TASK_STANDBY":
                    return RobotStatus.Normal;
                default:
                    return RobotStatus.Normal;
            }
        }

        //====================================================================================================
        //以上是获取车辆状态
        //----------------------------------------------------------------------------------------------------
        //以下是重置车辆及通信状态背景色
        //====================================================================================================

        private void ResetStatusColor()
        {
            switch (status)
            {
                case RobotStatus.Executing:
                    vehicleStatusBorder.Background = Brushes.LawnGreen;
                    break;
                case RobotStatus.Free:
                    vehicleStatusBorder.Background = Brushes.DarkGreen;
                    break;
                case RobotStatus.Normal:
                    vehicleStatusBorder.Background = Brushes.BurlyWood;
                    break;
                default:
                    break;
            }
        }

        private void ResetPacketStatusColor()
        {
            switch(packetStatus)
            {
                case RobotPacketStatus.Empty:
                    carInfoBorder.Background = Brushes.Transparent;
                    break;
                case RobotPacketStatus.HasPack:
                    carInfoBorder.Background = Brushes.White;
                    break;
                case RobotPacketStatus.HasPackDest:
                    carInfoBorder.Background = Brushes.Orange;
                    break;
                case RobotPacketStatus.NoInfo:
                    carInfoBorder.Background = Brushes.Red;
                    break;
                case RobotPacketStatus.NoRead:
                    carInfoBorder.Background = Brushes.Yellow;
                    break;
                case RobotPacketStatus.MaxCircle:
                    carInfoBorder.Background = Brushes.Blue;
                    break;
                default:
                    carInfoBorder.Background = Brushes.Transparent;
                    break;
            }
        }

        //====================================================================================================
        //以上是重置车辆及通信状态背景色
        //----------------------------------------------------------------------------------------------------
        //以下是实现IRectable接口
        //====================================================================================================

        Rect IRectable.GetBounds()
        {
            Rect rect;
            this.TryGetActualBounds(Location, out rect);
            return rect;
        }
    }
}
